package com.micangcaihang.micang.http;

import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.HttpMethod;
import com.lzy.okgo.model.HttpParams;
import com.micangcaihang.common.http.ApiResult;
import com.micangcaihang.common.okgo.RxUtils;
import com.micangcaihang.common.utils.MapBeanConvert;
import com.micangcaihang.micang.entity.BusinessRemarkEntity;
import com.micangcaihang.micang.entity.CommentaryExamineEntity;
import com.micangcaihang.micang.entity.FTaskBondsmanInfoEntity;
import com.micangcaihang.micang.entity.FTaskBorrowerInfoEntity;
import com.micangcaihang.micang.entity.FTaskCommonBorrowInfoEntity;
import com.micangcaihang.micang.entity.FTaskCommonOwnerInfoEntity;
import com.micangcaihang.micang.entity.FTaskCostDetailEntity;
import com.micangcaihang.micang.entity.FTaskFaceOpinionLoanConditionEntity;
import com.micangcaihang.micang.entity.FTaskLoansInfoEntity;
import com.micangcaihang.micang.entity.FTaskPracticalUserInfoEntity;
import com.micangcaihang.micang.entity.FTaskProductInfoEntity;
import com.micangcaihang.micang.entity.FileListEntity;
import com.micangcaihang.micang.entity.MatesEntity;
import com.micangcaihang.micang.entity.MatesRequest;
import com.micangcaihang.micang.entity.ReBackEntity;
import com.micangcaihang.micang.entity.TaskListEntity;
import com.micangcaihang.micang.entity.UserEntity;
import com.micangcaihang.micang.entity.UserInfoSp;
import com.micangcaihang.micang.http.request.ApplyStopRequest;
import com.micangcaihang.micang.http.request.BackTaskRequest;
import com.micangcaihang.micang.http.request.FTaskBorrowerInfoRequest;
import com.micangcaihang.micang.http.request.FTaskLoansInfoRequest;
import com.micangcaihang.micang.http.request.FTaskProductInfoRequest;
import com.micangcaihang.micang.util.HttpUtil;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/08/02
 *     update : 2017/08/02
 *     desc   : 风控服务器信息
 *     version: 1.0
 * </pre>
 */

public class FApiService {

    /**
     * 登录
     *
     * @param username 用户名
     * @param password 密码
     * @param <T>
     * @return
     */
    public static <T> Observable<T> user_logon(String username, String password) {
        Type type = new TypeToken<ApiResult<UserEntity>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("username", username);
        params.put("password", password);
        return RxUtils.request(HttpMethod.POST, ApiConstant.USER_LOGIN, type, params);
    }

    /**
     * 风控 - 获取新任务列表
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> risk_inhand() {
        Type type = new TypeToken<ApiResult<List<TaskListEntity>>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("userid", UserInfoSp.getInstance().getUserid());
        return RxUtils.request(HttpMethod.GET, ApiConstant.RISK_INHAND, type, params);
    }

    /**
     * Feng_Kong - 12 获取退回列表
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> risk_returns() {
        Type type = new TypeToken<ApiResult<List<TaskListEntity>>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("userid", UserInfoSp.getInstance().getUserid());
        return RxUtils.request(HttpMethod.GET, ApiConstant.RISK_RETURNS, type, params);
    }

    /**
     * 风控 - 02 获取贷款信息
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> risk_loan(String id) {
        Type type = new TypeToken<ApiResult<FTaskLoansInfoEntity>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("id", id);
        return RxUtils.request(HttpMethod.GET, ApiConstant.RISK_LOAN, type, params);
    }

    /**
     * 风控 -  09 保存/编辑(1贷款信息,2产品信息,3面审意见,4费用明细)
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> risk_save_loan(FTaskLoansInfoRequest request) {
        Type type = new TypeToken<ApiResult<Void>>() {
        }.getType();
        Map<String, String> map = MapBeanConvert.objectToMapString(request);
        HttpParams params = new HttpParams();
        params.put(map);
        params.put("type", "1");
        return RxUtils.request(HttpMethod.POST, ApiConstant.RISK_SAVE_LOAN, type, params);
    }

    /**
     * 风控 - 03 获取产品信息
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> risk_product(String id) {
        Type type = new TypeToken<ApiResult<FTaskProductInfoEntity>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("id", id);
        return RxUtils.request(HttpMethod.GET, ApiConstant.RISK_PRODUCT, type, params);
    }

    /**
     * 风控 -  09 保存/编辑(1贷款信息,2产品信息,3面审意见,4费用明细)
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> risk_save_loan(FTaskProductInfoRequest request) {
        Type type = new TypeToken<ApiResult<Void>>() {
        }.getType();
        Map<String, String> map = MapBeanConvert.objectToMapString(request);
        HttpParams params = new HttpParams();
        params.put(map);
        params.put("type", "2");
        return RxUtils.request(HttpMethod.POST, ApiConstant.RISK_SAVE_LOAN, type, params);
    }

    /**
     * 风控 - 04 获取借款人信息
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> risk_loaner(String id) {
        Type type = new TypeToken<ApiResult<FTaskBorrowerInfoEntity>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("id", id);
        return RxUtils.request(HttpMethod.GET, ApiConstant.RISK_LOANER, type, params);
    }

    /**
     * 10保存/编辑借款人信息
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> risk_save_loaner(FTaskBorrowerInfoRequest request) {
        Type type = new TypeToken<ApiResult<Void>>() {
        }.getType();
        HttpParams params = new HttpParams();
        Map<String, String> map = MapBeanConvert.objectToMapString(request);
        params.put(map);
        List<MatesRequest> mates = request.getMates();
        for (int i = 0; i < mates.size(); i++) {
            Map<String, String> stringStringMap = HttpUtil.objectToMapStringArray(mates.get(i), "mates" + "[" + mates.get(i).getRelationship_type() + "]");
            params.put(stringStringMap);
        }
        params.put("customer_type", "1");
        return RxUtils.request(HttpMethod.POST, ApiConstant.RISK_SAVE_LOANER, type, params);
    }

    /**
     * 风控 - 05 获取共有人,担保人,实际用款人,共借人信息
     * 人员类型(2共有人3担保人4共借人5实际用款人)
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> risk_customer(String id, String customer_type) {
        Type type = null;
        switch (customer_type) {
            case "2":
                type = new TypeToken<ApiResult<List<FTaskCommonOwnerInfoEntity>>>() {
                }.getType();
                break;
            case "3":
                type = new TypeToken<ApiResult<List<FTaskBondsmanInfoEntity>>>() {
                }.getType();
                break;
            case "4":
                type = new TypeToken<ApiResult<List<FTaskCommonBorrowInfoEntity>>>() {
                }.getType();
                break;
            case "5":
                type = new TypeToken<ApiResult<List<FTaskPracticalUserInfoEntity>>>() {
                }.getType();
                break;
        }
        HttpParams params = new HttpParams();
        params.put("id", id);
        params.put("customer_type", customer_type);
        return RxUtils.request(HttpMethod.GET, ApiConstant.RISK_CUSTOMER, type, params);
    }

    /**
     * 11 保存/编辑共有人,实际用款人,共借人,担保人信息
     * (2共有人,3担保人,4共借人,5实际用款人,)
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> risk_save_owners(List<FTaskCommonOwnerInfoEntity> requests, String id) {
        Type type = new TypeToken<ApiResult<Void>>() {
        }.getType();
        HttpParams params = new HttpParams();
        for (int i = 0; i < requests.size(); i++) {
            FTaskCommonOwnerInfoEntity request = requests.get(i);
            Map<String, String> stringStringMap = HttpUtil.objectToMapStringArray(request, "customer" + "[" + (i + 1) + "]");

            params.put(stringStringMap);
            List<MatesEntity> mates = request.getMates();
            for (int j = 0; j < mates.size(); j++) {
                Map<String, String> stringStringMap1 = HttpUtil.objectToMapStringArray(mates.get(j), "customer" + "[" + (i + 1) + "]" + "[mates]" + "[" + mates.get(j).getRelationship_type() + "]");
                params.put(stringStringMap1);
            }
        }
        params.put("customer_type", "2");
        params.put("id", id);
        return RxUtils.request(HttpMethod.POST, ApiConstant.RISK_SAVE_OWNERS, type, params);
    }

    /**
     * 12 保存/编辑共借人
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> risk_save_partner(List<FTaskCommonBorrowInfoEntity> requests, String id) {
        Type type = new TypeToken<ApiResult<Void>>() {
        }.getType();
        HttpParams params = new HttpParams();
        for (int i = 0; i < requests.size(); i++) {
            FTaskCommonBorrowInfoEntity request = requests.get(i);
            Map<String, String> stringStringMap = HttpUtil.objectToMapStringArray(request, "customer" + "[" + (i + 1) + "]");

            params.put(stringStringMap);
            List<MatesEntity> mates = request.getMates();
            for (int j = 0; j < mates.size(); j++) {
                Map<String, String> stringStringMap1 = HttpUtil.objectToMapStringArray(mates.get(j), "customer" + "[" + (i + 1) + "]" + "[mates]" + "[" + mates.get(j).getRelationship_type() + "]");
                params.put(stringStringMap1);
            }
        }
        params.put("customer_type", "4");
        params.put("id", id);
        return RxUtils.request(HttpMethod.POST, ApiConstant.RISK_SAVE_PARTNER, type, params);
    }

    /**
     * 12 保存/编辑实际用款人
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> risk_save_useiner(List<FTaskPracticalUserInfoEntity> requests, String id) {
        Type type = new TypeToken<ApiResult<Void>>() {
        }.getType();
        HttpParams params = new HttpParams();
        for (int i = 0; i < requests.size(); i++) {
            FTaskPracticalUserInfoEntity request = requests.get(i);
            Map<String, String> stringStringMap = HttpUtil.objectToMapStringArray(request, "customer" + "[" + (i + 1) + "]");

            params.put(stringStringMap);
            List<MatesEntity> mates = request.getMates();
            for (int j = 0; j < mates.size(); j++) {
                Map<String, String> stringStringMap1 = HttpUtil.objectToMapStringArray(mates.get(j), "customer" + "[" + (i + 1) + "]" + "[mates]" + "[" + mates.get(j).getRelationship_type() + "]");
                params.put(stringStringMap1);
            }
        }
        params.put("customer_type", "5");
        params.put("id", id);
        return RxUtils.request(HttpMethod.POST, ApiConstant.RISK_SAVE_USEINER, type, params);
    }

    /**
     * 12 保存/编辑担保人
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> risk_save_sponsor(List<FTaskBondsmanInfoEntity> requests, String id) {
        Type type = new TypeToken<ApiResult<Void>>() {
        }.getType();
        HttpParams params = new HttpParams();
        for (int i = 0; i < requests.size(); i++) {
            FTaskBondsmanInfoEntity request = requests.get(i);
            Map<String, String> stringStringMap = HttpUtil.objectToMapStringArray(request, "customer" + "[" + (i + 1) + "]");

            params.put(stringStringMap);
            List<MatesEntity> mates = request.getMates();
            for (int j = 0; j < mates.size(); j++) {
                Map<String, String> stringStringMap1 = HttpUtil.objectToMapStringArray(mates.get(j), "customer" + "[" + (i + 1) + "]" + "[mates]" + "[" + mates.get(j).getRelationship_type() + "]");
                params.put(stringStringMap1);
            }
        }
        params.put("customer_type", "5");
        params.put("id", id);
        return RxUtils.request(HttpMethod.POST, ApiConstant.RISK_SAVE_SPONSOR, type, params);
    }

    /**
     * 风控 -  09 保存/编辑(1贷款信息,2产品信息,3面审意见,4费用明细)
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> risk_save_loan(FTaskFaceOpinionLoanConditionEntity request) {
        Type type = new TypeToken<ApiResult<Void>>() {
        }.getType();
        Map<String, String> map = MapBeanConvert.objectToMapString(request);
        HttpParams params = new HttpParams();
        params.put(map);
        params.put("type", "3");
        return RxUtils.request(HttpMethod.POST, ApiConstant.RISK_SAVE_LOAN, type, params);
    }

    /**
     * 风控 - 04 获取借款人信息
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> risk_terms(String id) {
        Type type = new TypeToken<ApiResult<FTaskFaceOpinionLoanConditionEntity>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("id", id);
        return RxUtils.request(HttpMethod.GET, ApiConstant.RISK_TERMS, type, params);
    }

    /**
     * 07 获取费用明细
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> risk_money(String id) {
        Type type = new TypeToken<ApiResult<FTaskCostDetailEntity>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("id", id);
        return RxUtils.request(HttpMethod.GET, ApiConstant.RISK_MONEY, type, params);
    }

    /**
     * 风控 -  09 保存/编辑(1贷款信息,2产品信息,3面审意见,4费用明细)
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> risk_save_loan(FTaskCostDetailEntity request) {
        Type type = new TypeToken<ApiResult<Void>>() {
        }.getType();
        Map<String, String> map = MapBeanConvert.objectToMapString(request);
        HttpParams params = new HttpParams();
        params.put(map);
        params.put("type", "4");
        return RxUtils.request(HttpMethod.POST, ApiConstant.RISK_SAVE_LOAN, type, params);
    }

    /**
     * 07 01获取工单的业务备注
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> biz_course(String id) {
        Type type = new TypeToken<ApiResult<List<BusinessRemarkEntity>>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("id", id);
        return RxUtils.request(HttpMethod.GET, ApiConstant.BIZ_COURSE, type, params);
    }

    /**
     * 2申请终止
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> biz_app_end(ApplyStopRequest request) {
        Type type = new TypeToken<ApiResult<Void>>() {
        }.getType();
        Map<String, String> map = MapBeanConvert.objectToMapString(request);
        HttpParams params = new HttpParams();
        params.put(map);
        return RxUtils.request(HttpMethod.POST, ApiConstant.BIZ_APP_END, type, params);
    }

    /**
     * 03任务退回
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> biz_reback(BackTaskRequest request) {
        Type type = new TypeToken<ApiResult<Void>>() {
        }.getType();
        Map<String, String> map = MapBeanConvert.objectToMapString(request);
        HttpParams params = new HttpParams();
        params.put(map);
        return RxUtils.request(HttpMethod.POST, ApiConstant.BIZ_REBACK, type, params);
    }

    /**
     * Feng_Kong - 08 获取附件信息
     *
     * @param <T>
     * @param file_type (1借款人信息2借款人婚姻3借款用途相关资料4房屋证明5共有人6共借人7担保人8一抵9租赁10其他资料15实际用款人)
     * @return
     */
    public static <T> Observable<T> risk_files(String id, String file_type) {
        Type type = new TypeToken<ApiResult<FileListEntity>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("id", id);
        params.put("file_type", "2");
        params.put("file_category", file_type);
        return RxUtils.request(HttpMethod.GET, ApiConstant.RISK_FILES, type, params);
    }

    /**
     * Feng_Kong - 16 提交
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> risk_submit(String id, String reback) {
        Type type = new TypeToken<ApiResult<Void>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("id", id);
        params.put("reback", reback);
        params.put("userid", UserInfoSp.getInstance().getUserid());
        return RxUtils.request(HttpMethod.POST, ApiConstant.RISK_SUBMIT, type, params);
    }

    /**
     * Diao_Ping - 7 工单退回原因
     * 部门(2风控3调评)
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> biz_returnreason(String id, String step, String type) {
        Type type1 = new TypeToken<ApiResult<List<ReBackEntity>>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("id", id);
        params.put("step", step);
        params.put("type", type);
        return RxUtils.request(HttpMethod.GET, ApiConstant.BIZ_RETURNREASON, type1, params);
    }

    /**
     * 风控 产品类型计算费用
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> risk_product_temp(String product_name, String loan_type) {
        Type type1 = new TypeToken<ApiResult<FTaskProductInfoEntity>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("product_name", product_name);
        params.put("loan_type", loan_type);
        return RxUtils.request(HttpMethod.GET, ApiConstant.RISK_PRODUCT_TEMP, type1, params);
    }

    /**
     * 风控模块中资料审查调评信息
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> biz_invest(String id) {
        Type type = new TypeToken<ApiResult<CommentaryExamineEntity>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("id", id);
        return RxUtils.request(HttpMethod.GET, ApiConstant.BIZ_INVEST, type, params);
    }

    /**
     * 删除用户
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> risk_delete_customer(String customer_id) {
        Type type = new TypeToken<ApiResult<Void>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("customer_id", customer_id);
        return RxUtils.request(HttpMethod.GET, ApiConstant.DELETE_CUSTOMER, type, params);
    }
}
